#!/bin/bash
# Grep the reported errors from the files in arguments.
# Sum up the number of the reported errors.

total_file=0
total_ok_file=0
sp_cnt=0
tp_cnt=0
sg_cnt=0
ml_cnt=0
error_file=0 # and error_files
error_ok_file=0
noerr_file=0 # and noerr_files
noerr_ok_file=0
empty_file=0 # and empty_files
empty_ok_file=0
nonex_file=0 # and nonex_files

for file in $*; do
  flag=${file##*-} # of the form *-flag.error.txt
  flag=${flag%.error.txt}

  total_file=$(expr ${total_file} + 1)
  if [ "${flag}" = "ok" ]; then total_ok_file=$(expr ${total_ok_file} + 1); fi

  if [ -s ${file} ]; then
    spatial=$(cat ${file} | grep "\[spatial error\]" | wc -l)
    temporl=$(cat ${file} | grep "\[temporal error\]" | wc -l)
    segment=$(cat ${file} | grep "\[segment error\]" | wc -l)
    memleak=$(cat ${file} | grep "error: memory leak" | wc -l)
    sp_cnt=$(expr ${sp_cnt} + ${spatial})
    tp_cnt=$(expr ${tp_cnt} + ${temporl})
    sg_cnt=$(expr ${sg_cnt} + ${segment})
    ml_cnt=$(expr ${ml_cnt} + ${memleak})
    if [ "${spatial}" != "0" ] || [ "${temporl}" != "0" ] ||
       [ "${segment}" != "0" ] || [ "${memleak}" != "0" ]; then
      error_file=$(expr ${error_file} + 1)
      error_files="${error_files} ${file}"
      if [ "${flag}" = "ok" ]; then error_ok_file=$(expr ${error_ok_file} + 1); fi
    else
      noerr_file=$(expr ${noerr_file} + 1)
      noerr_files="${noerr_files} ${file}"
      if [ "${flag}" = "ok" ]; then noerr_ok_file=$(expr ${noerr_ok_file} + 1); fi
    fi
  elif [ -f ${file} ]; then
    empty_file=$(expr ${empty_file} + 1)
    empty_files="${empty_files} ${file}"
    if [ "${flag}" = "ok" ]; then empty_ok_file=$(expr ${empty_ok_file} + 1); fi
  else
    nonex_file=$(expr ${nonex_file} + 1)
    nonex_files="${nonex_files} ${file}"
  fi
done

if [ "${error_file}" != "0" ]; then
echo "========== Programs with errors detected are as follows."
for file in ${error_files}; do
  echo ${file%.error.txt}
done
fi

if [ "${noerr_file}" != "0" ]; then
echo "========== Programs WITHOUT errors detected are as follows."
for file in ${noerr_files}; do
  echo ${file%.error.txt}
done
fi

if [ "${empty_file}" != "0" ]; then
echo "========== Please manually run the following" ${empty_file} "programs to check for memory errors, as their output files are empty."
for file in ${empty_files}; do
  echo ${file%.error.txt}
done
fi

if [ "${nonex_file}" != "0" ]; then
echo "========== The following output files do not exist."
for file in ${nonex_files}; do
  echo ${file}
done
fi

error_cnt=$(expr ${sp_cnt} + ${tp_cnt} + ${sg_cnt} + ${ml_cnt})
msg="${error_cnt} memory errors"
if [ "${sp_cnt}" != "0" ] || [ "${tp_cnt}" != "0" ] ||
   [ "${sg_cnt}" != "0" ] || [ "${ml_cnt}" != "0" ]; then
    msg=${msg}" ("
    if [ "${sp_cnt}" != "0" ]; then msg=${msg}" SP=${sp_cnt}"; fi
    if [ "${tp_cnt}" != "0" ]; then msg=${msg}" TP=${tp_cnt}"; fi
    if [ "${sg_cnt}" != "0" ]; then msg=${msg}" SG=${sg_cnt}"; fi
    if [ "${ml_cnt}" != "0" ]; then msg=${msg}" ML=${ml_cnt}"; fi
    msg=${msg}" )"
fi
echo "========================================"
echo "There are" ${total_file} "programs in total (of which" ${total_ok_file} "are labeled as OK). ${msg} have been detected in the output files generated by the above" ${error_file} "programs (of which" ${error_ok_file} "are labeled as OK). No error has been detected in" ${noerr_file} "programs (of which" ${noerr_ok_file} "are labeled as OK). The remaining" ${empty_file} "programs (of which" ${empty_ok_file} "are labeled as OK) have produced empty output files (subject to manual run), possibly due to segmentation faults, special command-line options, environment variables or input files."

# Write to the specified file.
if [ -n "${BENCHMARK_TABLE_FILE}" ]; then
  echo -n "${error_file},${error_ok_file},$(expr ${noerr_file} + ${empty_file}),$(expr ${noerr_ok_file} + ${empty_ok_file})," >> ${BENCHMARK_TABLE_FILE}
fi
